<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      title="合并联系人"
      :async="true"
      width="40%"
      :clickModalClose="true"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="150px" :inline="true">
        <el-form-item label="合并后的联系人名称" prop="caseType">
          <el-select
            v-model="formData.name"
            placeholder="输入内容查询联系人"
            filterable
            class="w-[300px]"
            @change="handleFromdata"
          >
            <el-option
              v-for="item in linkmanData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人名称" prop="name">
          <el-input class="w-[300px]" v-model="formData.name" disabled />
        </el-form-item>
        <el-form-item label="工作地址" prop="workAddress">
          <el-input class="w-[300px]" v-model="formData.workAddress" disabled />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input class="w-[300px]" v-model="formData.phone" disabled />
        </el-form-item>
        <el-form-item label="工作邮箱" prop="workEmail">
          <el-input class="w-[300px]" v-model="formData.workEmail" disabled />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input class="w-[300px]" v-model="formData.position" disabled />
        </el-form-item>
        <el-form-item label="座机" prop="number">
          <el-input class="w-[300px]" v-model="formData.number" disabled />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { linkmanAdd, linkmanAlls, linkmanEdit } from '@/api/linkman'
import Popup from '@/components/popup/index.vue'
import type { PropType } from 'vue'
import { CUSTOMER_ID } from '@/enums/cacheEnums'
import cache from '@/utils/cache'
import feedback from '@/utils/feedback'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()

const formData = reactive({
  caseType: [],
  id: '',
  name: '',
  workEmail: '',
  customerMianId: '',
  position: '',
  email: '',
  isOnJob: '',
  callName: '',
  sex: '',
  type: '',
  number: '',
  phone: '',
  fax: '',
  qq: '',
  wechat: '',
  sort: '',
  workAddress: '',
  oldAddress: '',
  homeAddress: '',
  postcode: '',
  hobby: '',
  remark: ''
})

const queryParams = reactive({
  name: '',
  customerMianId: ''
})

const linkmanData = ref([])

const getList = async () => {
  queryParams.customerMianId = cache.get(CUSTOMER_ID)
  const data = await linkmanAlls(queryParams)
  linkmanData.value = data
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const handleFromdata = async (val) => {
  console.log(val)
  const data = linkmanData.value.filter((item) => {
    return item.id == val
  })
  console.log(data)
  setFormData(data[0])
}

const handleSubmit = async () => {
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = () => {
  popupRef.value?.open()
  getList()
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open
})
</script>
